package com.example.system.class13;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2022/8/6
 * @Author Jonathan
 * @Description 最大快乐值
 */
public class Code09_MaxHappy {


    public static void main(String[] args) {

    }

    public static int maxHappy(Employee employee) {
        final Info process = process(employee);
        return Math.max(process.no, process.yes);
    }


    public static Info process(Employee x) {
        if (x == null) {
            return new Info(0, 0);
        }

        int no = 0;
        int yes = x.happy;
        for (Employee next : x.nexts) {
            final Info process = process(next);
            yes += process.no; // x节点是yes的话 后代肯定都是no
            no += Math.max(process.no, process.yes); //取后代来或者不来的最大值即可
        }
        return new Info(no, yes);


    }


    static class Info {
        int yes;
        int no;

        public Info(int yes, int no) {
            this.yes = yes;
            this.no = no;
        }
    }


    static class Employee {
        int happy;
        List<Employee> nexts;

        public Employee(int happy) {
            this.happy = happy;
            this.nexts = new ArrayList<>();
        }
    }


}
